%% FFT Example

%% Setup
tic
clear all;
close all;



Fs=100;	% Sampling Freq
Ts=1/Fs;	% Sample time
L=600;		% Number of samples
t=(0:L-1)*Ts; %Time Vector

% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
y = sin(2*pi*45*t) + cos(2*pi*4*t) + rand(size(t));% Sinusoids plus noise
figure(1);
plot(Fs*t,y)
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')

%% Compute FFT
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs*linspace(0,.5,NFFT/2+1);
amp=2*abs(Y(1:NFFT/2+1));

% Plot single-sided amplitude spectrum.
figure(2);
plot(f,amp) 
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
grid on